

let BaseView = require('../BaseView');

let templateDiv = `
           <label>动画信息</label>
           <div class="inspect_animation"></div>
`;


let UIConstants = require('../ui_constants');
let RotateByView = require('../anims/RotateByView');
let ScaleToView = require('../anims/ScaleToView');
let MoveByView = require('../anims/MoveByView');
let settings = require('../../settings');

class InspAnimView extends BaseView{
    constructor(){
        super();

        this.rootEl = document.createElement('div');
        this.rootEl.innerHTML = templateDiv;

        // 初始化动画信息
        this.animContentEl = this.rootEl.querySelector('.inspect_animation');
        this.initDragAndDrop();

        // 存放动画之前的属性
        // this.initData = null;
    }

    initCaches(){

        // view cache
        this.viewCache = {};

        let rotateByView = new RotateByView(); // eslint-disable-line
        this.viewCache[UIConstants.AnimTemplate.RotateBy] = rotateByView;

        let scaleToView = new ScaleToView();
        this.viewCache[UIConstants.AnimTemplate.ScaleTo] = scaleToView;

        let moveByView = new MoveByView();
        this.viewCache[UIConstants.AnimTemplate.MoveBy] = moveByView;

        // dataCache  动画前的数据
        this.dataCache = {};
    }

    clearUI(){
        this.animContentEl.innerHTML = '';
    }

    saveInitData(){
        let inspectView = editor.getInspectView();
        let selectedSp = inspectView.getSelectedSp();
        if(selectedSp){
            this.dataCache[selectedSp.id] = myTools.deepClone(inspectView.getModel());
        }
    }

    // 还原数据给 selectData
    restoreInitData(){
        let inspectView = editor.getInspectView();
        let selectedSp = inspectView.getSelectedSp();
        if(selectedSp && this.dataCache[selectedSp.id]){
            let model = inspectView.getModel();
            let data = this.dataCache[selectedSp.id];
            for(let k in data){
                model[k] = data[k];
            }
        }
    }

    // getInitData(){
    //     return this.initData;
    // }

    initDragAndDrop(){
        this.animContentEl.addEventListener('dragenter',(e)=>{
            e.stopPropagation();
            e.preventDefault();
        },false);

        this.animContentEl.addEventListener('dragover',(e)=>{
            e.stopPropagation();
            e.preventDefault();
        },false);

        this.animContentEl.addEventListener('drop',(e)=>{
            e.stopPropagation();
            e.preventDefault();
            this.onDrop(e);
        },false);
    }


    onDrop(e){
        let dt = e.dataTransfer;
        let animType = Number(dt.getData('template_anim'));
        if(Number.isNaN(animType)){
            return;
        }

        // 判断当前是否已经有动画数据
        let inspectView = editor.getInspectView();
        let model = inspectView.getModel();
        if(model.anims){
            settings.alert('已经有了动画数据!');
            return;
        }

        let selectedSp = inspectView.getSelectedSp();
        switch (animType) {
            case UIConstants.AnimTemplate.RotateBy:
                let rotateByView = this.viewCache[animType];// eslint-disable-line
                this.animContentEl.appendChild(rotateByView.rootEl);
                rotateByView.parentView = this;
                selectedSp.anims = model.anims = {
                    tempId:animType,
                    dx:rotateByView.model.angle,
                    dy:0,
                    time:rotateByView.model.time,
                };
                rotateByView.updateInfoLabel();
                break;
            case UIConstants.AnimTemplate.ScaleTo:
                let scaleToView = this.viewCache[animType];// eslint-disable-line
                this.animContentEl.appendChild(scaleToView.rootEl);
                scaleToView.parentView = this;
                selectedSp.anims = model.anims = {
                    tempId:animType,
                    dx:scaleToView.model.sx,
                    dy:scaleToView.model.sy,
                    time:scaleToView.model.time,
                };
                scaleToView.updateInfoLabel();
                break;
            case UIConstants.AnimTemplate.MoveBy:
                let moveByView = this.viewCache[animType]; // eslint-disable-line
                this.animContentEl.appendChild(moveByView.rootEl);
                moveByView.parentView = this;
                selectedSp.anims = model.anims = {
                    tempId:animType,
                    dx:moveByView.model.dx,
                    dy:moveByView.model.dy,
                    time:moveByView.model.time,
                };
                moveByView.updateInfoLabel();
                break;
            default:
                throw new Error(`unknow animType:${animType}`);
        }
    }

    updateUI(animData){
        this.clearUI();

        if(animData){
            let animType = animData.tempId;
            let dx = animData.dx;
            let dy = animData.dy;
            let time = animData.time;

            switch (animType) {
                case UIConstants.AnimTemplate.RotateBy:
                    let rotateByView = this.viewCache[animType];// eslint-disable-line
                    this.animContentEl.appendChild(rotateByView.rootEl);
                    rotateByView.parentView = this;
                    rotateByView.model.angle = dx;
                    rotateByView.model.time = time;
                    rotateByView.updateInfoLabel();
                    break;
                case UIConstants.AnimTemplate.ScaleTo:
                    let scaleToView = this.viewCache[animType]; // eslint-disable-line
                    this.animContentEl.appendChild(scaleToView.rootEl);
                    scaleToView.parentView = this;
                    scaleToView.model.sx = dx;
                    scaleToView.model.sy = dy;
                    scaleToView.model.time = time;
                    scaleToView.updateInfoLabel();
                    break;
                case UIConstants.AnimTemplate.MoveBy:
                    let moveByView = new MoveByView(); // eslint-disable-line
                    this.animContentEl.appendChild(moveByView.rootEl);
                    moveByView.parentView = this;
                    moveByView.model.dx = dx;
                    moveByView.model.dy = dy;
                    moveByView.model.time = time;
                    moveByView.updateInfoLabel();
                    break;
                default:
                    throw new Error(`unknow animType:${animType}`);
            }
        }
    }
}
module.exports = InspAnimView;
